package org.mistfx.mvp.demo.dashboard;

import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import org.mistfx.mvp.demo.dashboard.light.LightView;

import javax.inject.Inject;
import java.net.URL;
import java.time.LocalDate;
import java.util.ResourceBundle;

/**
 * @author Misty
 */
public class DashboardPresenter implements Initializable {

    @FXML
    AnchorPane root;

    @FXML
    Label message;

    @FXML
    Pane lightsBox;

    @Inject
    Tower tower;

    @Inject
    private String prefix;

    @Inject
    private String happyEnding;

    @Inject
    private LocalDate date;

    private String theVeryEnd;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        //fetched from followme.properties
        this.theVeryEnd = rb.getString("theEnd");
    }

    public void createLights() {
        for (int i = 0; i < 255; i++) {
            final int red = i;
            LightView view = new LightView((f) -> red);
            view.getViewAsync(lightsBox.getChildren()::add);
        }
    }

    @FXML
    public void launch() {
        message.setText("Date: " + date + " -> " + prefix +
                tower.readyToTakeoff() + happyEnding + theVeryEnd);
    }

    public void hide() { // debug
        Scene scene = root.getScene();
        Parent savedRoot = root.getScene().getRoot();
        Button btn = new Button("Show");
        btn.setOnAction(event -> {
            scene.setRoot(savedRoot);
//            System.out.println("--- " + root.getScene());
        });
        scene.setRoot(new Group(btn));
//        System.out.println("--- " + root.getScene());
    }

    public void gc() { // debug
        System.gc();
    }

    public void transfer() { // debug
        Scene scene = root.getScene();
        Stage savedStage = (Stage) scene.getWindow();

        Stage stage = new Stage();
        stage.setScene(scene);
        stage.show();
    }
}
